<?php
/**
**票付通对外接的全部在这里处理
**2015-04-10 青岛海底世界
**2015-04-30 携程
**2015-05-11 爱森斯
**2015-05-27 畅游通
**2015-05-29 江西瑞金
**2015-06-08 智游宝
**2015-06-26 天时同城
**2015-07-02 燕塞湖
**2015-07-07 山海关
**by pzp
**/
include('/var/www/html/new/d/module/api/function.class.php');

if (!class_exists("go_sql")){
	include("/var/www/html/new/conf/le.je");
	$le=new go_sql();
	$le->connect();
}

$ac='16ucom';
$pw='c33367701511b4f6020ec61ded352059';
$inSide = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));

// $retime1 = microtime(true); 

$txt=file_get_contents('php://input');

$arr = base64_decode($txt);
$arrs1 = explode('#',$arr);
$arrs2 = explode('|',$arrs1[1]);
$method = $arrs1[0];
$Check = $arrs2[0];
$order_arr = json_decode($arrs2[1],true);
$order_arr_id = $order_arr[0]['id'];
$order = $order_arr[0]['orderid'];
$methoda = substr($arr,0,5);
$sign = md5(($order_arr_id + $order).$methoda); 

logfile($arrs2[1],'all_api_data2.txt');

if($Check == $sign){
	foreach($order_arr as $orders){ 
		$order_true = $orders['orderid']; //票付通订单号
		$pft_order = $inSide->Order_Globle_Search(
								0,0,0,0,"",
								"","","","","",
								"","",$order_true,"",0,
								"","","",1,0,
								0,1,"","","",
								"","","","","",
								"","","1","","");
		$pft_order_xml =simplexml_load_string($pft_order);
		$UUlid = (int)$pft_order_xml->Rec->UUlid;  				//景区id
		$UUtid = (int)$pft_order_xml->Rec->UUtid;				//门票id
		$UUltitle =(string)$pft_order_xml->Rec->UUltitle; 		//景区名称
		$UUttitle =(string)$pft_order_xml->Rec->UUttitle;		//门票名称
		$ordername = (string)$pft_order_xml->Rec->UUordername;	//订单客人姓名
		$UUtnum = (int)$pft_order_xml->Rec->UUtnum;				//订单数量
		$UUremotenums = explode("_",(string)$pft_order_xml->Rec->UUremotenum);		
		$UUremotenum = $UUremotenums[1];//远端订单号
		$UUpid=(int)$pft_order_xml->Rec->UUpid;					//门票pid
		$UUordertime=(string)$pft_order_xml->Rec->UUordertime;  //下单时间
		$UUplaytime=(string)$pft_order_xml->Rec->UUbegintime;	//游玩时间
		$UUendtime=(string)$pft_order_xml->Rec->UUendtime;		//结束时间
		$UUtprice=(string)$pft_order_xml->Rec->UUtprice;		//结算价
		$UUordertel=(string)$pft_order_xml->Rec->UUordertel;	//订单客人手机号码
		$UUstatus = (int)$pft_order_xml->Rec->UUstatus;			//订单状态
		$IDcar =(string)$pft_order_xml->Rec->UUpersonid;		//订单客人身份证号
		$UUcode =(string)$pft_order_xml->Rec->UUcode;			//凭证码	
		$UUdname =(string)$pft_order_xml->Rec->UUdname;		//分销商名称		
		$UUtotalmoney =(string)$pft_order_xml->Rec->UUtotalmoney;		//总金额
		$UUfromt =(string)$pft_order_xml->Rec->UUfromt;		//总金额		
		$UUaids=(string)$pft_order_xml->Rec->UUaids;
		if($UUaids == 0){
			$buyid = (string)$pft_order_xml->Rec->UUbuyid;
			$UUaid = (string)$pft_order_xml->Rec->UUaid;			
		}else{
			$UUaidss= explode(',',$UUaids);
			$buyid = $UUaidss[1];
			$UUaid = $UUaidss[0];
		}
		
		$select_uuid = "select uuid,tprice from uu_jq_ticket where id=".$UUtid." limit 1";
		$GLOBALS['le']->query($select_uuid);
		$row = $GLOBALS['le']->fetch_assoc();
		$uuid = $row['uuid'];  //查询商品编码id
		$tprice = $row['tprice']; //门市价
		
		$PFT_infunction = new AllFunction(); 
		
		$select_gprice = "select account from pft_member where id=".$UUaid." limit 1";
		$GLOBALS['le']->query($select_gprice);
		$row2 = $GLOBALS['le']->fetch_assoc();
		$aaccount = $row2['account'];
		$get_gprice = $PFT_infunction->Dynamic_Price_And_Storage($aaccount,$UUpid,$UUplaytime,0,$UUaid);
		$get_gprice_xml = simplexml_load_string($get_gprice);
		$gprice = (string)$get_gprice_xml->Rec->UUsprice / 100; //供货价
		
		$get_lsprice = $PFT_infunction->Dynamic_Price_And_Storage($UUfromt,$UUpid,$UUplaytime,1,$UUaid);
		$get_lsprice_xml = simplexml_load_string($get_lsprice);
		$UUlprice = (string)$get_lsprice_xml->Rec->UUsprice / 100; //零售价
		
		// $retime2 = microtime(true);
		
		/*method：TK79-推送订单,MD89-修改订单,取消订单*/
		if($method =='TK79'){
			$pay_status = $orders['pay_status'];  //0未支付，1已支付
			$request_array = array(
				'pftordernum' =>$order_true,
				'pftordername' =>$ordername,
				'UUtnum' =>$UUtnum,
				'UUordertime'=>$UUordertime,
				'UUplaytime'=>$UUplaytime,
				'UUendtime'=>$UUendtime,
				'UUdname'=>$UUdname,
				'UUtprice'=>$UUtprice / 100,
				'UUordertel'=>$UUordertel,
				'UUtotalmoney'=>$UUtotalmoney,
				'uuid'=>$uuid,	
				'IDcar'=>$IDcar,
				'productname'=>$UUttitle,
				'UUltitle'=>$UUltitle,
				'UUcode'=>$UUcode,
				'UUlprice'=>$UUlprice,
				'ispay' => $pay_status,	
				'gprice'=> $gprice,
				'tprice'=> $tprice,
			);
			switch($UUlid){
				//微信及短信通知
				case 5057:	//摩锐水世界
				case 8813:	//五台山
				case 6992:	//厦门观音山
				case 7418:
				case 7012:
				case 6990:
				case 2876:
				case 12580:
				case 12728:
                case 7304:
				case 12669:
					include_once('/var/www/html/new/d/class/OrderSmsManual.class.php');
					$smsManual = new \PFT\SMS\OrderSmsManual($inSide);
					$proInfos['mainOrder'] = $pft_order_xml->Rec;
					$note = $smsManual->OrderNotify('AFTER_PAY', $proInfos);
					logfile(date('Y-m-d H:i:s').'|'.$note,'all_api_data.txt');
					if($note == 1) echo 100;
				break;
				
				// 瑞金-叶坪	(九天)
				// 瑞金-二苏大	(九天)
				// 瑞金-红井  	(九天)
				case 8048:  
				case 8055:  
				case 8054:  
					include_once('RuiJin/ruijin.class.php');
					$RJ = new RunJin($UUlid);
					$request = $RJ->submitOrder($request_array);
					if($request == 101){ 
						echo 101;
					}
					else{
						$str = "update uu_order_fx_details set memo=100 where orderid=".$order_true." limit 1";
						$GLOBALS['le']->query($str);
						echo 100;
					}
				break;
				
				case 10180: //百合-孟姜女
				case 10179: //百合-山海关天下第一关
				case 10178:
				case 10185:
				case 10234:
				case 10251:
				case 9904:
				case 9909:
				case 9911:
				case 10292:
				case 10293:
					include_once 'ShanHaiGuan/shanhaiguan.class.php';
					$shg = new shanhaiguan($UUlid);
					$createOrder = $shg->createOrder($request_array);
					if($createOrder == 101)
						echo 101;
					else{
						$updateRemote = $inSide->Remote_Ordernum_Update(
							serialize(array($order_true=>'shg_'.$createOrder))
						);
						echo 100;
					}
				break;			
				/* case 9895://翡翠岛-百合
				case 10423://翡翠岛-海源
                    include_once '/var/www/html/new/d/module/qkxproject/api/feicd/feicd.class.php';
                    $feicd = new FeiCD($UUlid);
                    $request = $feicd->Agent_AddOrder($request_array);
                    if($request == 101){
                        echo 101;
                    }
                    else{
                        $updateRemote = $inSide->Remote_Ordernum_Update(
                            serialize(array($order_true=>$request))
                        );
                        echo 100;
                    }
                break;
		
				case 9062:
				case 9060:
				case 9395:
				case 10680:
                    include_once '/var/www/html/new/d/module/qkxproject/api/zushan/zushan.class.php';
                    $zushan = new Zushan($UUlid);
                    $request = $zushan->SubmitOrder($request_array);
                    if($request == 101){
                        echo 101;
                    }
                    else{
                        $updateRemote = $inSide->Remote_Ordernum_Update(
                            serialize(array($order_true=>'zs_'.$request))
                        );
                        $str = "update uu_order_fx_details set memo='zs_success' where orderid=".$order_true." limit 1";
                        $GLOBALS['le']->query($str);
                        echo 100;
                    }
                break;
				
				case 10980://百合
					include_once '/var/www/html/new/d/module/qkxproject/api/shenyouhuaxia/syhx.class.php';
					$syhx = new ShenYouHuaXia($UUlid);
					$request = $syhx->submitOrder($request_array);

					if($request == 101){
						echo 101;
					}
					else{
						echo 100;
					}
				break; 
				
				case 9415: //百合-燕塞湖
				case 8533: //海源-燕塞湖  
					include_once 'YanSaiHu/yansaihu.class.php';
					$ysh = new YanSaiHu($UUlid);
					$request_array = array(
						"UUordertel"=>$UUordertel,
						"UUtnum"=>$UUtnum,
					);
					$oSaveRCode = $ysh->oSaveRCode($request_array);
					if($oSaveRCode == 101) echo 101;  //下单失败
					else{ //下单成功
						$updateRemote = $inSide->Remote_Ordernum_Update(
							serialize(array($order_true=>'ysh_'.$oSaveRCode))
						);
						echo 100;
					}
				break;
				
				//鸡冠洞的景区订单
				//重渡沟的景区订单
				case 8062:  
				case 8085:  
					include_once 'AiSiSen/aisisen_class.php';
					$ass = new AiSiSen($UUlid);  //爱森斯
					logfile(json_encode($request_array),'all_api_data2.txt');
					$request = $ass->agencyOrderAdd($request_array);
					if($request == 101){ 
						echo 101;
					}
					else{
						$updateRemote = $inSide->Remote_Ordernum_Update(
							serialize(array($order_true=>'ass_'.(string)$request->platOrderNo))
						);
						echo 100;
					}
				break;
				
				*/
				
				default:
				break;				
			}			
			
		}
	}
}

// $retime5 = microtime(true);
// $retime6 = $retime5 - $retime1 ;  
// logfile('runtime: '.$retime6,'all_api_data2.txt');

// function logfile($txt,$path){
	// $fp = fopen($path,"a");
	// flock($fp, LOCK_EX) ;
	// fwrite($fp,$txt."\n");
	// flock($fp, LOCK_UN);
	// fclose($fp); 
// }

function logfile($word,$path){
	$filepath= '/var/www/html/new/d/logs/api/api_cancel/'.$path.'.txt';
	if(file_exists($filepath)){
		$word = date("Y-m-d H:i:s")."|".$word;
		$word = mb_convert_encoding($word, 'GBK', 'UTF-8');
		$maxsize  = 1024*1024*10;
		$filesize = filesize($filepath);
		$filetype = end(explode(".",$filepath));
		$filename = substr($filepath,0,strpos($filepath,'.'));
		if($filesize>$maxsize)
		{
			rename($filepath, $filename.'_'.date('YmdHis').'.'.$filetype);
			$handler=fopen($filepath,'w');
			flock($handler, LOCK_EX) ;
			fwrite($handler,"\n".$word);
			flock($handler, LOCK_UN);
			fclose($handler);
		}
		else
		{
			$fp = fopen('/var/www/html/new/d/logs/api/api_cancel/'.$path.'.txt',"a");
			flock($fp, LOCK_EX);
			fwrite($fp,$word."\n");
			flock($fp, LOCK_UN);
			fclose($fp);
		}
	}else{
		$fp = fopen('/var/www/html/new/d/logs/api/api_cancel/'.$path.'.txt',"a");
		flock($fp, LOCK_EX);
		fwrite($fp,date('Y-m-d H:i:s')."|".$word."\n");
		flock($fp, LOCK_UN);
		fclose($fp);
	}
}
?>